package com.itheima.controller;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.common.Result;
import com.itheima.dto.DishDto;
import com.itheima.dto.SetmealDto;
import com.itheima.entity.Category;
import com.itheima.entity.Dish;
import com.itheima.entity.Setmeal;
import com.itheima.service.CategoryService;
import com.itheima.service.SetmealDishService;
import com.itheima.service.SetmealService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 套餐管理
 *
 * @author 偏爱即浪漫
 */
@Slf4j
@RestController
@RequestMapping("/setmeal")
@Api(tags = "套餐相关接口")
public class SetmealController {
    @Autowired
    private SetmealService setmealService;
    @Autowired
    private SetmealDishService setmealDishService;
    @Autowired
    private CategoryService categoryService;

    /*
    分页查询套餐数据
    */
    @GetMapping("/page")
    @ApiOperation(value = "套餐分页查询接口")
    public Result<Page> page(int page, int pageSize, String name) {
        //new一个page对象
        Page<Setmeal> pageinfo = new Page(page, pageSize);
        Page<SetmealDto> pagedto = new Page<>();
        //构建条件查询器
        LambdaQueryWrapper<Setmeal> queryWrapper = new LambdaQueryWrapper();
        queryWrapper.like(StringUtils.isNotEmpty(name), Setmeal::getName, name);
        queryWrapper.orderByDesc(Setmeal::getUpdateTime);
        //调用service中的page方法传递参数
        setmealService.page(pageinfo, queryWrapper);
        BeanUtils.copyProperties(pageinfo, pagedto, "records");
////        单独拿出records
        List<Setmeal> records = pageinfo.getRecords();
        List<SetmealDto> collect = records.stream().map((iteam) -> {
            SetmealDto setmealDto = new SetmealDto();
//            //第二遍复制，将单独取出的records复制到setmealdish中
            BeanUtils.copyProperties(iteam, setmealDto);
////            从iteam中取出id
            Long id = iteam.getCategoryId();
            Category category = categoryService.getById(id);
            String categoryName = category.getName();
            setmealDto.setCategoryName(categoryName);
            return setmealDto;
        }).collect(Collectors.toList());
//        //给pagedto设置records
        pagedto.setRecords(collect);
        return Result.success(pagedto);
    }
    /*
    根据条件查询套餐信息
     */
    @GetMapping("/list")
    @Cacheable(value = "setmealCache",key = "#setmeal.categoryId+'_'+#setmeal.status")
    @ApiOperation(value = "套餐条件查询接口")
    public Result<List<Setmeal>> list(Setmeal setmeal){
        LambdaQueryWrapper<Setmeal> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(setmeal.getCategoryId()!=null,Setmeal::getCategoryId,setmeal.getCategoryId());
        queryWrapper.eq(setmeal.getStatus()!=null,Setmeal::getStatus,setmeal.getStatus());
        List<Setmeal> list = setmealService.list(queryWrapper);
        return Result.success(list);
    }

    /*
    新增套餐
     */
    @PostMapping
    @CacheEvict(value = "setmealCache",allEntries = true)
    @ApiOperation(value = "新增套餐接口")
    public Result<String > saveSetmeal(@RequestBody SetmealDto setmealDto){
        setmealService.saveWithDish(setmealDto);
        return Result.success("新增套餐信息成功！");
    }
    /*
    根接id查询菜品信息
    */
    @GetMapping("/{id}")
    @ApiOperation(value = "id查询菜品信息接口")
    public Result<SetmealDto> getById(@PathVariable Long id) {
        log.info(id.toString());
        SetmealDto byIdDish = setmealService.getByIdDish(id);
        return Result.success(byIdDish);
    }
    /*
    修改套餐信息
     */
    @PutMapping
    @CacheEvict(value = "setmealCache",allEntries = true)
    @ApiOperation(value = "套餐修改接口")
    public Result<String> update(HttpServletRequest servletRequest, @RequestBody SetmealDto setmealDto) {
        log.info("{}",setmealDto);
        setmealService.updateWithDish(setmealDto);
        return Result.success("菜品信息修改成功！");
    }
    /*
    删除套餐信息
     */
    @DeleteMapping
    @CacheEvict(value = "setmealCache",allEntries = true)
    @ApiOperation(value = "套餐删除接口")
    public Result<String> delete(@RequestParam List<Long> ids){
        setmealService.removeWithDish(ids);
        return Result.success("删除成功！");
    }
    /*
  批量修改套餐状态信息
  */
    @PostMapping("/status/{params}")
    @CacheEvict(value = "setmealCache",allEntries = true)
    @ApiOperation(value = "套餐状态修改接口")
    public Result<String> updateStatus(@PathVariable("params") Integer status, @RequestParam List<Long> ids) {

        LambdaQueryWrapper<Setmeal> queryWrapper = new LambdaQueryWrapper();
        queryWrapper.in(ids!=null,Setmeal::getId,ids);
        //根据数据进行批量查询
        List<Setmeal> list = setmealService.list(queryWrapper);

        for (Setmeal setmeal : list) {
            if (setmeal != null) {
                setmeal.setStatus(status);
                setmealService.updateById(setmeal);
            }
        }
        return Result.success("套餐状态信息已修改！");
    }
}
